#include "stdafx.h"
#include "CGrass.h"
#include <fstream>
#include <iostream>
using namespace std;

CGrass::CGrass()
{
}


CGrass::~CGrass()
{
}

void CGrass::InitGrass()
{
	m_grassimg = cvLoadImage("./picture/grass0.jpg");
}

void CGrass::ReadGrasstxt()
{
	fstream grasspos;
	grasspos.open("./Map/grassloc.txt");
	for (int i = 0; i < 11; i++)
	{
		for (int j = 0; j < 11; j++)
		{
			grasspos >> m_grassloc[i][j];
		}
	}
	grasspos.close();
}

void CGrass::DrawtoBk(IplImage * pbk)
{
	if (pbk != NULL && m_grassimg != NULL)
	{
		for (int i = 0; i < m_grassimg->height; i++)
		{
			for (int j = 0; j < m_grassimg->width; j++)
			{
				int bch = CV_IMAGE_ELEM(m_grassimg, uchar, i, j * 3 + 0);
				int gch = CV_IMAGE_ELEM(m_grassimg, uchar, i, j * 3 + 1);
				int rch = CV_IMAGE_ELEM(m_grassimg, uchar, i, j * 3 + 2);
				if (!(bch > 245 && gch > 245 && rch > 245))
				{
					CV_IMAGE_ELEM(pbk, uchar, m_pos.x*60 + i, (j + m_pos.y*60) * 3 + 0) = CV_IMAGE_ELEM(m_grassimg, uchar, i, j * 3 + 0);
					CV_IMAGE_ELEM(pbk, uchar, m_pos.x*60 + i, (j + m_pos.y*60) * 3 + 1) = CV_IMAGE_ELEM(m_grassimg, uchar, i, j * 3 + 1);
					CV_IMAGE_ELEM(pbk, uchar, m_pos.x*60 + i, (j + m_pos.y*60) * 3 + 2) = CV_IMAGE_ELEM(m_grassimg, uchar, i, j * 3 + 2);
				}
			}
		}
	}
}

void CGrass::ReleaseImg()
{
	cvReleaseImage(&m_grassimg);
}

void CGrass::DrawGrassDie(IplImage * pbk)
{
	m_grassdieimg = cvLoadImage("./picture/map0.jpg");
	if (pbk != NULL && m_grassdieimg != NULL)
	{
		for (int i = 0; i < m_grassdieimg->height; i++)
		{
			for (int j = 0; j < m_grassdieimg->width; j++)
			{
				int bch = CV_IMAGE_ELEM(m_grassdieimg, uchar, i, j * 3 + 0);
				int gch = CV_IMAGE_ELEM(m_grassdieimg, uchar, i, j * 3 + 1);
				int rch = CV_IMAGE_ELEM(m_grassdieimg, uchar, i, j * 3 + 2);
				CV_IMAGE_ELEM(pbk, uchar, m_pos.x * 60 + i, (j + m_pos.y * 60) * 3 + 0) = CV_IMAGE_ELEM(m_grassdieimg, uchar, i, j * 3 + 0);
				CV_IMAGE_ELEM(pbk, uchar, m_pos.x * 60 + i, (j + m_pos.y * 60) * 3 + 1) = CV_IMAGE_ELEM(m_grassdieimg, uchar, i, j * 3 + 1);
				CV_IMAGE_ELEM(pbk, uchar, m_pos.x * 60 + i, (j + m_pos.y * 60) * 3 + 2) = CV_IMAGE_ELEM(m_grassdieimg, uchar, i, j * 3 + 2);
			}
		}
	}
}
